package com.gpay.erp.web.control;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.dao.page.PageHelper;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.CostType;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.services.system.CostInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.UserSessionInfor;

/**
 * 费用类别管理
 * 
 * @author lg
 * 
 */
@Controller
@RequestMapping("/costController/")
public class CostController extends BaseController {
	private Log log = LogFactory.getLog(CostController.class);
	@Autowired
	private CostInteface costService;

	// 页面跳转 费用类别管理
	@RequestMapping("selectCost")
	public String selectCost() {
		return "system/cost/selectCost";
	}

	// 页面跳转 费用类别新增
	@RequestMapping("addCost")
	public String addcost() {
		return "system/cost/addCost";
	}

	// 页面跳转 费用类别修改
	@RequestMapping("updateCost")
	public String updatecost() {
		return "system/cost/updateCost";
	}

	/*
	 * 费用类别管理，进行分页查询
	 * 
	 * @param pageHelper
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectCostType")
	public void selectCostType(PagerModel pagerModel, CostType costType,
			PageHelper pageHelper, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 当前页 */
		int pagenow = pageHelper.getPage();
		/* 每页显示记录数 */
		int rows = pageHelper.getRows();
		/* 分页信息填充进入PagerModel中 */
		costType.setPageSize(rows);
		costType.setFromRow((pagenow - 1) * rows);
		costType.setToRow(pagenow * rows);
		/* 分页查询，返回PagerModel */
		pagerModel = costService.selectPageList(costType);
		log.info("selectCost end");
		if (pagerModel.getRows().size() > 0) {
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加费用类别信息
	 * 
	 * @param costType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addCostType")
	public void addCostType(CostType costType, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取登录的用户信息 */
		SysUser loginUser = (SysUser) session.getAttribute("loginUser");
		/* 设置日期格式 */
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/* 获取当地时间 */
		costType.setCreateDate(df.format(new Date()));
		costType.setCreateName(loginUser.getUserName());
		int flag = costService.insert(costType);// 执行新增操作，返回t_cost_type表新增行的id
		if (flag != 0) {
			log.info("插入记录后的id值为：" + flag);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看银行卡详细信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailCostType")
	public void detailCostType(PagerModel pagerModel, CostType costType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		if (costType.getId() != null) {
			pagerModel = costService.selectById(costType.getId());// 根据ID查询明细
			if (pagerModel != null) {
				responseInfo.setResult("00010");
				responseInfo.setMessage(requestContext
						.getMessage("select.success"));
			} else {
				responseInfo.setResult("00011");
				responseInfo.setMessage(requestContext
						.getMessage("select.fail"));
			}
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改费用类别信息
	 * 
	 * @param costType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateCostType")
	public void updateCostType(CostType costType, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		if (costType.getId() != null) {
			/* 获取登录的用户信息 */
			SysUser loginUser = (SysUser) session.getAttribute("loginUser");
			/* 设置日期格式 */
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			/* 获取当地时间 */
			costType.setCreateDate(df.format(new Date()));
			costType.setUpdateName(loginUser.getUserName());
			int id = costService.update(costType);// 执行修改操作，返回t_cost_type表修改行的id
			if (id != 0) {
				log.info("修改记录的id值为：" + id);
				responseInfo.setResult("00030");
				responseInfo.setMessage(requestContext
						.getMessage("update.success"));
			} else {
				responseInfo.setResult("00031");
				responseInfo.setMessage(requestContext
						.getMessage("update.fail"));
			}
		} else {
			responseInfo.setResult("00031");
			responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击删除，根据id删除费用类别信息
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deleteCostType")
	public void deleteCostType(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		String[] ids = request.getParameterValues("ids");// 接收前端传输的id值
		/* String [] ids={"4","8"}; */
		if (ids != null) {
			int dels = costService.deletes(ids);// 执行删除操作
			if (dels != 0) {
				responseInfo.setResult("00020");
				responseInfo.setMessage(requestContext
						.getMessage("delete.success"));
			} else {
				responseInfo.setResult("00021");
				responseInfo.setMessage(requestContext
						.getMessage("delete.fail"));
			}
		} else {
			responseInfo.setResult("00021");
			responseInfo.setMessage(requestContext.getMessage("delete.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

}
